package cn.wolfcode.controller;

import cn.wolfcode.annotation.LoginAnnotation;
import cn.wolfcode.annotation.ParamAnnotation;
import cn.wolfcode.cache.IStrategyCommentRedisService;
import cn.wolfcode.cache.vo.StrategyStatisVO;
import cn.wolfcode.domain.*;
import cn.wolfcode.query.StrategyCommentQuery;
import cn.wolfcode.query.StrategyQuery;
import cn.wolfcode.service.*;
import cn.wolfcode.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("strategies")
public class StrategyController {

    @Autowired
    private IStrategyCommentRedisService strategyCommentRedisService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyService strategyService;

    @GetMapping("detail")
    public Object detail(String id){
        Strategy strategy = strategyService.get(id);
        strategyCommentRedisService.viewnumIntercrea(id,1);
        return JsonResult.success(strategy);
    }

    @GetMapping("query")
    public Object query(StrategyQuery qo){
        Page query = strategyService.query(qo);
        return JsonResult.success(query);
    }
    @GetMapping("themes")
    public Object themes(){
        List<StrategyTheme> list = strategyThemeService.listAll();
        return JsonResult.success(list);
    }

    @LoginAnnotation
    @PostMapping("addComment")
    public Object addComment(StrategyComment comment, @ParamAnnotation UserInfo userInfo){
        //属性拷贝
        //参数一: 源对象 参数二:目标对象
        //字段名必须要一致
        BeanUtils.copyProperties(userInfo,comment);
        comment.setUserId(userInfo.getId());
        comment.setCreateTime(new Date());
        strategyCommentService.insert(comment);
        strategyCommentRedisService.replynumIncrease(comment.getStrategyId(),1);
        return JsonResult.success();
    }

    @GetMapping("comments")
    public Object comments(StrategyCommentQuery qo){
        return JsonResult.success(strategyCommentService.query(qo));
    }

    @LoginAnnotation
    @PostMapping("commentThumb")
    public Object commentThumb(String cid,@ParamAnnotation UserInfo userInfo){
        strategyCommentService.commentThumb(cid,userInfo);
        return JsonResult.success();
    }
    @GetMapping("statisVo")
    public Object statisVo(String sid){
        StrategyStatisVO vo = strategyCommentRedisService.getViewnum(sid);
        return JsonResult.success(vo);
    }
    @LoginAnnotation
    @PostMapping("favor")
    public Object favor(String sid,@ParamAnnotation UserInfo userInfo){
        boolean flag = strategyCommentRedisService.favorIncrease(sid,userInfo.getId());
        return JsonResult.success(flag);
    }

    @LoginAnnotation
    @PostMapping("strategyThumbup")
    public Object strategyThumbup(String sid,@ParamAnnotation UserInfo userInfo){
        boolean flag = strategyCommentRedisService.thumbupIncrease(sid,userInfo.getId());
        return JsonResult.success(flag);
    }
}
